package vcp.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import vcp.application.UserManagement;
import vcp.common.AbstractController;
import vcp.common.IController;
import vcp.models.RatesModel;
import vcp.views.ParkingWorkerRatesView;

/**
 * Rates Controller
 *
 */
public class RatesController extends AbstractController implements ActionListener,AncestorListener, IController {
    private RatesModel ratesModel;
    private ParkingWorkerRatesView ratesView;
	private Object ratesTableRowListener;
	
	@Override
	public void addModel(Object model) {
		this.ratesModel = (RatesModel)model;		
	}

	@Override
	public void addView(Object view) {
		this.ratesView = (ParkingWorkerRatesView)view;	
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public Object getTableListselectionListener(){
		if(ratesTableRowListener==null){
			ratesTableRowListener = new RatesTableRowListener(this.ratesView.getPnlrates().getTable());
		}
		return ratesTableRowListener;
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		int lotNum = UserManagement.getInstance().getEmployee().getLotNo();
		this.ratesModel.getRatesByLotNum(lotNum);
		System.out.println(event.getAncestor());		
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
		// TODO Auto-generated method stub
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
		// TODO Auto-generated method stub
		
	}

}



class RatesTableRowListener implements ListSelectionListener {
	private JTable table=null;
	public RatesTableRowListener(JTable table) {
		this.table = table;
	}

	public JTable getTable() {
		return table;
	}

	public void setTable(JTable table) {
		this.table = table;
	}
	public void valueChanged(ListSelectionEvent event) {
        if (event.getValueIsAdjusting()) {
            return;
        }
        String selectedData = null;
        JOptionPane.showMessageDialog(null,this.table.getValueAt(event.getFirstIndex(), 0));
       // System.out.println("Selected: " + table.getValueAt(event.getFirstIndex(), 2));
       
    }
}	


